package com.client.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;


import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;


import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.border.StandardBorderPainter;
import org.jvnet.substance.button.StandardButtonShaper;
import org.jvnet.substance.skin.SubstanceBusinessBlackSteelLookAndFeel;
import org.jvnet.substance.theme.SubstanceAquaTheme;
import org.jvnet.substance.title.FlatTitlePainter;
import org.jvnet.substance.watermark.SubstanceBinaryWatermark;

import com.client.ctrl.LoginCtrl;







public class LoginFrame  {
	
	static JFrame loginFrame=null;

	public static void createLoginFrame() {
		loginFrame=new JFrame();
		Image image=Toolkit.getDefaultToolkit().getImage("src/com/util/logal.jpg");
		loginFrame.setIconImage(image);
		
		loginFrame.setBounds(600, 200, 800,600);
		loginFrame.setTitle("欢迎登陆中信教务系统");
		loginFrame.setFont(new Font(null,Font.BOLD,48));
		loginFrame.setResizable(false);
		loginFrame.setVisible(true);
		loginFrame.setLayout(null);
		
		final ImageIcon logalIcon=new ImageIcon("src/com/util/logal.jpg");
		logalIcon.setImage(logalIcon.getImage().getScaledInstance(60,60,Image.SCALE_DEFAULT));
		JLabel titleLabel=new JLabel();
		titleLabel.setIcon(logalIcon);
		titleLabel.setText("<html><h><font color='white'>中信教务系统</font></h></html>");
		
		titleLabel.setBounds(210,60,380,60);
		titleLabel.setFont(new Font(null,Font.BOLD,48));
		titleLabel.setForeground(Color.white);
		loginFrame.add(titleLabel);
		
		JPanel contentPanel=(JPanel)loginFrame.getContentPane();
		contentPanel.setOpaque(false);
		
		
		contentPanel.add(createPanel());
		
		
		final JLabel label=new JLabel();
		final ImageIcon icon=new ImageIcon("src/com/util/login.jpg");
		icon.setImage(icon.getImage().getScaledInstance(800,600,Image.SCALE_DEFAULT));

		label.setIcon(icon); // ���屳��
		label.setBounds(0,0, icon.getIconWidth(),icon.getIconHeight());

		
		
		
		loginFrame.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));
		loginFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
		
		
		loginFrame.setVisible(true);
	}
	
	public static JPanel createPanel(){
		JPanel loginPanel=new JPanel();
		loginPanel.setLayout(null);
		
		
		JLabel titleLabel=new JLabel();
		Font titleFont=new Font(null, Font.BOLD, 30);
		titleLabel.setFont(titleFont);
		titleLabel.setForeground(Color.gray);
		titleLabel.setBounds(140,35,130,40);
		titleLabel.setText("欢迎登录");
		loginPanel.add(titleLabel);
		
		
		JLabel accountLabel=new JLabel();
		Font font=new Font(null, Font.BOLD, 18);
		accountLabel.setFont(font);
		accountLabel.setForeground(Color.gray);
		accountLabel.setBounds(60, 90,60, 30);
		accountLabel.setText("账户:");
		loginPanel.add(accountLabel);
	
       
	final JTextField accountField=new JTextField();
		accountField.setBounds(120,90, 200, 30);
	    accountField.addFocusListener(new FocusListener() {
			
			public void focusLost(FocusEvent e) {
				String account=accountField.getText().trim();
				
				String regex="\\d{10}";
				if(account.equals("")){
					
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>�˺Ų���Ϊ��</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
				}else if(!account.matches(regex)){
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>�˺ű���Ϊ10λ����</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);

				}
				
			}
			
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		loginPanel.add(accountField);
		
		JLabel passLabel=new JLabel();
		passLabel.setFont(font);
		passLabel.setForeground(Color.gray);
		passLabel.setText("密码:");
		passLabel.setBounds(60, 140, 60, 30);
		loginPanel.add(passLabel);
		
		final JPasswordField passField=new JPasswordField();
		passField.setBounds(120,140, 200, 30);
		
		loginPanel.add(passField);
		
		JRadioButton adminButton=new JRadioButton("管理员");
		adminButton.setActionCommand("管理员");
		adminButton.setFont(font);
		adminButton.setForeground(Color.darkGray);
		adminButton.setSelected(true);
		adminButton.setOpaque(false);
		adminButton.setBounds(60, 190,100, 30);
		loginPanel.add(adminButton);
		
		JRadioButton teacherButton=new JRadioButton("教师");
		teacherButton.setActionCommand("教师");
		teacherButton.setFont(font);
		teacherButton.setForeground(Color.darkGray);
		teacherButton.setOpaque(false);
		teacherButton.setBounds(160, 190,90, 30);
		loginPanel.add(teacherButton);
		
		JRadioButton studentButton=new JRadioButton("学生");
		studentButton.setActionCommand("学生");
		studentButton.setFont(font);
		studentButton.setForeground(Color.darkGray);
		studentButton.setOpaque(false);
		studentButton.setBounds(250, 190,90, 30);
		loginPanel.add(studentButton);
		
		ButtonGroup roleGroup=new ButtonGroup();
		roleGroup.add(adminButton);
		roleGroup.add(teacherButton);
		roleGroup.add(studentButton);
		
		final JButton loginButton=new JButton("登录");
		loginButton.setFont(new Font(null, Font.BOLD, 24));
		loginButton.setForeground(Color.blue);
		//loginButton.setBorder(new BevelBorder(BevelBorder.RAISED));
		loginButton.setBounds(60, 230, 260, 30);
		loginButton.addActionListener(new LoginCtrl(loginButton,loginFrame,accountField,passField,roleGroup));
		loginPanel.add(loginButton);
		
         passField.addFocusListener(new FocusListener() {
			
			public void focusLost(FocusEvent e) {
				String account=accountField.getText().trim();
				String regex1="\\d{10}";
				String regex2= "\\w{6,18}";
				String password=String.valueOf(passField.getPassword()).trim();
			
               if(account.equals("")){
            	   loginButton.setEnabled(false);
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>登录账号不能为空</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
				}else if(!account.matches(regex1)){
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>账号必须为10位数字组成</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
					loginButton.setEnabled(false);
				}else if(password.equals("")){
					loginButton.setEnabled(false);
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>密码不能为空</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
					
				}else if(!password.matches(regex2)){
					loginButton.setEnabled(false);
					JOptionPane.showMessageDialog(loginFrame,new JLabel("<html><h><font color='red'>密码必须为6-18位数字，字母或'_'组成</font></h></html>"), null, JOptionPane.ERROR_MESSAGE);
				}else{
					loginButton.setEnabled(true);
				}
				
			}
			
			public void focusGained(FocusEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		
		final ImageIcon icon=new ImageIcon("src/com/util/loginPanel.jpg");
		icon.setImage(icon.getImage().getScaledInstance(400,300,Image.SCALE_DEFAULT));
		JLabel iconLabel=new JLabel(icon);
		iconLabel.setBounds(0, 0, 400, 300);
		
		loginPanel.setBounds(200, 150, 400, 300);
		loginPanel.add(iconLabel,-1);
		return loginPanel;
	}
	
	public static void main(String[]args) {
	try {
		//�������
		UIManager.setLookAndFeel(new SubstanceBusinessBlackSteelLookAndFeel());
		JFrame.setDefaultLookAndFeelDecorated(true);
		//��������
		SubstanceLookAndFeel.setCurrentTheme(new SubstanceAquaTheme());
		//���ð�ť���
		SubstanceLookAndFeel.setCurrentButtonShaper(new StandardButtonShaper());
		//����ˮӡ
		SubstanceLookAndFeel.setCurrentWatermark(new SubstanceBinaryWatermark());
		//���ñ߿�
		SubstanceLookAndFeel.setCurrentBorderPainter(new StandardBorderPainter());
		//���ñ���
		SubstanceLookAndFeel.setCurrentTitlePainter(new FlatTitlePainter());
		
		//���ô����ڲ�Э��
		JFrame.setDefaultLookAndFeelDecorated(true);

		JDialog.setDefaultLookAndFeelDecorated(true);
	} catch (UnsupportedLookAndFeelException e) {
		System.out.println(e.getMessage());
	}
				createLoginFrame();
			
		
	}

}
